<?php
/**
 *
 * 版权所有：顺势网站管理系统<qwadmin.qiawei.com>

 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：客户案例控制器。
 *
 **/

namespace Qwadmin\Controller;

class AnliController extends ComController
{

    public function add()
    {
        $adviser = M('adviser');
        $list = $adviser->field("id,name")->select();
        $this->assign('list', $list);
        $this->display('form');
    }

    public function index($p = 1)
    {


        $p = intval($p) > 0 ? $p : 1;

        $anli = M('anli');
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
        $prefix = C('DB_PREFIX');
        $where = "{$prefix}anli.deleted = 0";
        $orderby = "{$prefix}anli.id ".$order;
        $count = $anli->where($where)->count();
        $list = $anli->where($where)
                ->field("{$prefix}anli.*,{$prefix}adviser.name")
                ->join("{$prefix}adviser ON {$prefix}adviser.id = {$prefix}anli.adviser_id")
                ->order($orderby)
                ->limit($offset . ',' . $pagesize)
                ->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del()
    {

        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if ($id) {
            $anli = M('anli');
            $map = 'id=' . $id;
            if ($anli->where($map)->delete()) {
                addlog('删除客户案例，ID：' . $id);
                $this->success('恭喜，客户案例删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }

    public function edit($id)
    {

        $id = intval($id);
        $anli = M('anli');
        $info = $anli->where('id=' . $id)->find();
        if ($info) {
            $adviser = M('adviser');
            $list = $adviser->field("id,name")->select();
            $this->assign('list', $list);
            $this->assign('article', $info);
        } else {
            $this->error('参数错误！');
        }
        $this->display('form');
    }

    public function update($id = 0)
    {

        $id = intval($id);
        $data['kh_name'] = isset($_POST['kh_name']) ? $_POST['kh_name'] : false;
        $data['dk_desc'] = isset($_POST['dk_desc']) ? $_POST['dk_desc'] : '';
        $data['ziliao'] = isset($_POST['ziliao']) ? $_POST['ziliao'] : '';
        $data['anli_desc'] = isset($_POST['anli_desc']) ? $_POST['anli_desc'] : false;
        $data['adviser_id'] = isset($_POST['adviser_id']) ? intval($_POST['adviser_id']) : 0;
        $data['kh_desc'] = isset($_POST['kh_desc']) ? $_POST['kh_desc'] : false;
//        pp($data);exit();
        if (!$data['kh_name'] or !$data['anli_desc']) {
            $this->error('警告！客户案例标题及客户案例内容为必填项目。');
        }
        if ($id) {
            M('anli')->data($data)->where('id=' . $id)->save();
            addlog('编辑客户案例，id：' . $id);
            $this->success('恭喜！客户案例编辑成功！');
        } else {
            $data['ctime'] = date("Y-m-d H:i:s",time());
            $id = M('anli')->data($data)->add();
            if ($id) {
                addlog('新增客户案例，ID：' . $id);
                $this->success('恭喜！客户案例新增成功！');
            } else {
                $this->error('抱歉，未知错误！');
            }

        }
    }
}